337B - Routine Problem - CodeForces Solution


greedy math number theory *1400

Please click on ads to support us..

Python Code:

def gcd(a, b):
    while a % b != 0:
        a, b = b, a % b
    return b

l = input().split(' ')
a, b, c, d = int(l[0]), int(l[1]), int(l[2]), int(l[3])
if a * d > b * c:
    p = a * d - b * c
    q = a * d
else:
    p = b * c - a * d
    q = b * c
e = gcd(p, q)
p //= e
q //= e
print(str(p) + "/" + str(q))

C++ Code:

#include <bits/stdc++.h>


using namespace std;

int main()
{
    int a,b,c,d;
    cin>>a>>b>>c>>d;
  // int minab=min(a,b);
 //  int maxab=max(a,b);
  // int mincd=min(c,d);
   //int maxcd=max(c,d);
 // double ans=1-(((mincd/maxcd)*minab)/maxab);
    //int x = __gcd(maxcd*maxab-minab*mincd,maxcd*maxab);
    //printf("%d/%d",(maxcd*maxab-minab*mincd)/x,(maxcd*maxab)/x);
  if(a*d>b*c){
      swap(a,b);swap(c,d);
  }
  int p=(b*c-a*d) ;
  int q=b*c;
  int x=__gcd(p,q);
  cout<<p/x<<"/"<<q/x;
    
}
   			 		      	 	   		   			


Comments

Submit
0 Comments
More Questions

254A - Cards with Numbers
215A - Bicycle Chain
1288B - Yet Another Meme Problem
1201C - Maximum Median
435A - Queue on Bus Stop
1409B - Minimum Product
723B - Text Document Analysis
1471C - Strange Birthday Party
1199A - City Day
1334A - Level Statistics
67B - Restoration of the Permutation
1734A - Select Three Sticks
1734B - Bright Nice Brilliant
357B - Flag Day
937A - Olympiad
1075A - The King's Race
1734C - Removing Smallest Multiples
1004C - Sonya and Robots
922A - Cloning Toys
817A - Treasure Hunt
1136B - Nastya Is Playing Computer Games
1388A - Captain Flint and Crew Recruitment
592B - The Monster and the Squirrel
1081A - Definite Game
721C - Journey
1400A - String Similarity
1734E - Rectangular Congruence
1312D - Count the Arrays
424C - Magic Formulas
1730C - Minimum Notation